home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / sunbird / components / nsProxyAutoConfig.js < prev    next >
Encoding:
Text File  |  2007-05-23  |  14.7 KB  |  444 lines

  1. /* -*- Mode: Java; tab-width: 4; c-basic-offset: 4; -*- */
  2. /* vim:set ts=4 sw=4 sts=4 et: */
  3. /* ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is mozilla.org code.
  17.  *
  18.  * The Initial Developer of the Original Code is
  19.  * Netscape Communications Corporation.
  20.  * Portions created by the Initial Developer are Copyright (C) 1998
  21.  * the Initial Developer. All Rights Reserved.
  22.  *
  23.  * Contributor(s):
  24.  *   Akhil Arora <akhil.arora@sun.com>
  25.  *   Tomi Leppikangas <Tomi.Leppikangas@oulu.fi>
  26.  *   Darin Fisher <darin@meer.net>
  27.  *
  28.  * Alternatively, the contents of this file may be used under the terms of
  29.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  30.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  31.  * in which case the provisions of the GPL or the LGPL are applicable instead
  32.  * of those above. If you wish to allow use of your version of this file only
  33.  * under the terms of either the GPL or the LGPL, and not to allow others to
  34.  * use your version of this file under the terms of the MPL, indicate your
  35.  * decision by deleting the provisions above and replace them with the notice
  36.  * and other provisions required by the GPL or the LGPL. If you do not delete
  37.  * the provisions above, a recipient may use your version of this file under
  38.  * the terms of any one of the MPL, the GPL or the LGPL.
  39.  *
  40.  * ***** END LICENSE BLOCK ***** */
  41.  
  42. /*
  43.    Script for Proxy Auto Config in the new world order.
  44.        - Gagan Saksena 04/24/00 
  45. */
  46.  
  47. const kDNS_CONTRACTID = "@mozilla.org/network/dns-service;1";
  48. const kPAC_CONTRACTID = "@mozilla.org/network/proxy-auto-config;1";
  49. const kPAC_CID = Components.ID("{63ac8c66-1dd2-11b2-b070-84d00d3eaece}");
  50.  
  51. const nsISupports        = Components.interfaces.nsISupports;
  52. const nsIProxyAutoConfig = Components.interfaces.nsIProxyAutoConfig;
  53. const nsIDNSService      = Components.interfaces.nsIDNSService;
  54.  
  55. // Loaded once per PAC script, this is a safe way for the supplied functions
  56. // that require chrome privileges to turn a random untrusted object into a
  57. // string.
  58. var safeToString = null;
  59. function myToString(thisp) {
  60.     return thisp + '';
  61. }
  62.  
  63. // This is like safeToString, except that it calls a given function with a
  64. // given this and arguments.
  65. var callFunction = null;
  66. function myCall(fun) {
  67.     var args = [];
  68.     for (var i = 1; i < arguments.length; i++)
  69.         args.push(arguments[i]);
  70.     return fun.apply(this, args);
  71. }
  72.  
  73. // Like the above, except that this gets a property off of an untrusted
  74. // object.
  75. var safeGetProperty = null;
  76. function myGet(thisp, id) {
  77.     return thisp[id];
  78. }
  79.  
  80. // implementor of nsIProxyAutoConfig
  81. function nsProxyAutoConfig() {};
  82.  
  83. nsProxyAutoConfig.prototype = {
  84.     // sandbox in which we eval loaded autoconfig js file
  85.     _sandBox: null, 
  86.  
  87.     // ptr to eval'ed FindProxyForURL function
  88.     _findProxyForURL: null,
  89.  
  90.     QueryInterface: function(iid) {
  91.         if (iid.Equals(nsIProxyAutoConfig) ||
  92.             iid.Equals(nsISupports))
  93.             return this;
  94.         throw Components.results.NS_ERROR_NO_INTERFACE;
  95.     },
  96.  
  97.     init: function(pacURI, pacText) {
  98.         // remove PAC configuration if requested
  99.         if (pacURI == "" || pacText == "") {
  100.             this._findProxyForURL = null;
  101.             this._sandBox = null;
  102.             return;
  103.         }
  104.  
  105.         // allocate a fresh Sandbox to clear global scope for new PAC script
  106.         this._sandBox = new Components.utils.Sandbox(pacURI);
  107.         Components.utils.evalInSandbox(pacUtils, this._sandBox);
  108.  
  109.         safeToString =
  110.             Components.utils.evalInSandbox("(" + myToString.toSource() + ")",
  111.                                            this._sandBox);
  112.         callFunction =
  113.             Components.utils.evalInSandbox("(" + myCall.toSource() + ")",
  114.                                            this._sandBox);
  115.  
  116.         // Clone callFunction.call onto our callFunction so that the PAC
  117.         // script can't monkey with Function.prototype.call and confuse us.
  118.         callFunction.call = Function.prototype.call;
  119.  
  120.         safeGetProperty =
  121.             Components.utils.evalInSandbox("(" + myGet.toSource() + ")",
  122.                                            this._sandBox);
  123.  
  124.         // add predefined functions to pac
  125.         this._sandBox.importFunction(myIpAddress);
  126.         this._sandBox.importFunction(dnsResolve);
  127.         this._sandBox.importFunction(proxyAlert, "alert");
  128.  
  129.         // evaluate loaded js file
  130.         Components.utils.evalInSandbox(pacText, this._sandBox);
  131.         this._findProxyForURL =
  132.             safeGetProperty(this._sandBox, "FindProxyForURL");
  133.     },
  134.  
  135.     getProxyForURI: function(testURI, testHost) {
  136.         if (!this._findProxyForURL)
  137.             return null;
  138.  
  139.         // Call the original function
  140.         return callFunction.call(this._sandBox, this._findProxyForURL,
  141.                                  testURI, testHost);
  142.     }
  143. }
  144.  
  145. function proxyAlert(msg) {
  146.     // Ensure that we have a string.
  147.     msg = safeToString(msg);
  148.  
  149.     try {
  150.         // It would appear that the console service is threadsafe.
  151.         var cns = Components.classes["@mozilla.org/consoleservice;1"]
  152.                             .getService(Components.interfaces.nsIConsoleService);
  153.         cns.logStringMessage("PAC-alert: "+msg);
  154.     } catch (e) {
  155.         dump("PAC: proxyAlert ERROR: "+e+"\n");
  156.     }
  157. }
  158.  
  159. // wrapper for getting local IP address called by PAC file
  160. function myIpAddress() {
  161.     try {
  162.         return dns.resolve(dns.myHostName, 0).getNextAddrAsString();
  163.     } catch (e) {
  164.         return '127.0.0.1';
  165.     }
  166. }
  167.  
  168. // wrapper for resolving hostnames called by PAC file
  169. function dnsResolve(host) {
  170.     host = safeToString(host);
  171.  
  172.     try {
  173.         return dns.resolve(host, 0).getNextAddrAsString();
  174.     } catch (e) {
  175.         return null;
  176.     }
  177. }
  178.  
  179. var pacModule = new Object();
  180.  
  181. pacModule.registerSelf =
  182.     function (compMgr, fileSpec, location, type) {
  183.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  184.         compMgr.registerFactoryLocation(kPAC_CID,
  185.                                         "nsProxyAutoConfig",
  186.                                         kPAC_CONTRACTID,
  187.                                         fileSpec, 
  188.                                         location, 
  189.                                         type);
  190.     }
  191.  
  192. pacModule.getClassObject =
  193. function (compMgr, cid, iid) {
  194.         if (!cid.equals(kPAC_CID))
  195.             throw Components.results.NS_ERROR_NO_INTERFACE;
  196.  
  197.         if (!iid.equals(Components.interfaces.nsIFactory))
  198.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  199.  
  200.         return pacFactory;
  201.     }
  202.  
  203. pacModule.canUnload =
  204.     function (compMgr) {
  205.         return true;
  206.     }
  207.  
  208. var pacFactory = new Object();
  209. pacFactory.createInstance =
  210.     function (outer, iid) {
  211.         if (outer != null)
  212.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  213.  
  214.         if (!iid.equals(nsIProxyAutoConfig) &&
  215.             !iid.equals(Components.interfaces.nsISupports)) {
  216.             throw Components.results.NS_ERROR_NO_INTERFACE;
  217.         }
  218.         return pac;
  219.     }
  220.  
  221. function NSGetModule(compMgr, fileSpec) {
  222.     return pacModule;
  223. }
  224.  
  225. var pac = new nsProxyAutoConfig() ;
  226. var dns = Components.classes[kDNS_CONTRACTID].getService(nsIDNSService);
  227.  
  228. var pacUtils = 
  229. "function dnsDomainIs(host, domain) {\n" +
  230. "    return (host.length >= domain.length &&\n" +
  231. "            host.substring(host.length - domain.length) == domain);\n" +
  232. "}\n" +
  233.  
  234. "function dnsDomainLevels(host) {\n" +
  235. "    return host.split('.').length-1;\n" +
  236. "}\n" +
  237.  
  238. "function convert_addr(ipchars) {\n"+
  239. "    var bytes = ipchars.split('.');\n"+
  240. "    var result = ((bytes[0] & 0xff) << 24) |\n"+
  241. "                 ((bytes[1] & 0xff) << 16) |\n"+
  242. "                 ((bytes[2] & 0xff) <<  8) |\n"+
  243. "                  (bytes[3] & 0xff);\n"+
  244. "    return result;\n"+
  245. "}\n"+
  246.  
  247. "function isInNet(ipaddr, pattern, maskstr) {\n"+
  248. "    var test = /^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$/(ipaddr);\n"+
  249. "    if (test == null) {\n"+
  250. "        ipaddr = dnsResolve(ipaddr);\n"+
  251. "        if (ipaddr == null)\n"+
  252. "            return false;\n"+
  253. "    } else if (test[1] > 255 || test[2] > 255 || \n"+
  254. "               test[3] > 255 || test[4] > 255) {\n"+
  255. "        return false;    // not an IP address\n"+
  256. "    }\n"+
  257. "    var host = convert_addr(ipaddr);\n"+
  258. "    var pat  = convert_addr(pattern);\n"+
  259. "    var mask = convert_addr(maskstr);\n"+
  260. "    return ((host & mask) == (pat & mask));\n"+
  261. "    \n"+
  262. "}\n"+
  263.  
  264. "function isPlainHostName(host) {\n" +
  265. "    return (host.search('\\\\.') == -1);\n" +
  266. "}\n" +
  267.  
  268. "function isResolvable(host) {\n" +
  269. "    var ip = dnsResolve(host);\n" +
  270. "    return (ip != null);\n" +
  271. "}\n" +
  272.  
  273. "function localHostOrDomainIs(host, hostdom) {\n" +
  274. "    return (host == hostdom) ||\n" +
  275. "           (hostdom.lastIndexOf(host + '.', 0) == 0);\n" +
  276. "}\n" +
  277.  
  278. "function shExpMatch(url, pattern) {\n" +
  279. "   pattern = pattern.replace(/\\./g, '\\\\.');\n" +
  280. "   pattern = pattern.replace(/\\*/g, '.*');\n" +
  281. "   pattern = pattern.replace(/\\?/g, '.');\n" +
  282. "   var newRe = new RegExp('^'+pattern+'$');\n" +
  283. "   return newRe.test(url);\n" +
  284. "}\n" +
  285.  
  286. "var wdays = new Array('SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT');\n" +
  287.  
  288. "var monthes = new Array('JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC');\n"+
  289.  
  290. "function weekdayRange() {\n" +
  291. "    function getDay(weekday) {\n" +
  292. "        for (var i = 0; i < 6; i++) {\n" +
  293. "            if (weekday == wdays[i]) \n" +
  294. "                return i;\n" +
  295. "        }\n" +
  296. "        return -1;\n" +
  297. "    }\n" +
  298. "    var date = new Date();\n" +
  299. "    var argc = arguments.length;\n" +
  300. "    var wday;\n" +
  301. "    if (argc < 1)\n" +
  302. "        return false;\n" +
  303. "    if (arguments[argc - 1] == 'GMT') {\n" +
  304. "        argc--;\n" +
  305. "        wday = date.getUTCDay();\n" +
  306. "    } else {\n" +
  307. "        wday = date.getDay();\n" +
  308. "    }\n" +
  309. "    var wd1 = getDay(arguments[0]);\n" +
  310. "    var wd2 = (argc == 2) ? getDay(arguments[1]) : wd1;\n" +
  311. "    return (wd1 == -1 || wd2 == -1) ? false\n" +
  312. "                                    : (wd1 <= wday && wday <= wd2);\n" +
  313. "}\n" +
  314.  
  315. "function dateRange() {\n" +
  316. "    function getMonth(name) {\n" +
  317. "        for (var i = 0; i < 6; i++) {\n" +
  318. "            if (name == monthes[i])\n" +
  319. "                return i;\n" +
  320. "        }\n" +
  321. "        return -1;\n" +
  322. "    }\n" +
  323. "    var date = new Date();\n" +
  324. "    var argc = arguments.length;\n" +
  325. "    if (argc < 1) {\n" +
  326. "        return false;\n" +
  327. "    }\n" +
  328. "    var isGMT = (arguments[argc - 1] == 'GMT');\n" +
  329. "\n" +
  330. "    if (isGMT) {\n" +
  331. "        argc--;\n" +
  332. "    }\n" +
  333. "    // function will work even without explict handling of this case\n" +
  334. "    if (argc == 1) {\n" +
  335. "        var tmp = parseInt(arguments[0]);\n" +
  336. "        if (isNaN(tmp)) {\n" +
  337. "            return ((isGMT ? date.getUTCMonth() : date.getMonth()) ==\n" +
  338. "getMonth(arguments[0]));\n" +
  339. "        } else if (tmp < 32) {\n" +
  340. "            return ((isGMT ? date.getUTCDate() : date.getDate()) == tmp);\n" +
  341. "        } else { \n" +
  342. "            return ((isGMT ? date.getUTCFullYear() : date.getFullYear()) ==\n" +
  343. "tmp);\n" +
  344. "        }\n" +
  345. "    }\n" +
  346. "    var year = date.getFullYear();\n" +
  347. "    var date1, date2;\n" +
  348. "    date1 = new Date(year,  0,  1,  0,  0,  0);\n" +
  349. "    date2 = new Date(year, 11, 31, 23, 59, 59);\n" +
  350. "    var adjustMonth = false;\n" +
  351. "    for (var i = 0; i < (argc >> 1); i++) {\n" +
  352. "        var tmp = parseInt(arguments[i]);\n" +
  353. "        if (isNaN(tmp)) {\n" +
  354. "            var mon = getMonth(arguments[i]);\n" +
  355. "            date1.setMonth(mon);\n" +
  356. "        } else if (tmp < 32) {\n" +
  357. "            adjustMonth = (argc <= 2);\n" +
  358. "            date1.setDate(tmp);\n" +
  359. "        } else {\n" +
  360. "            date1.setFullYear(tmp);\n" +
  361. "        }\n" +
  362. "    }\n" +
  363. "    for (var i = (argc >> 1); i < argc; i++) {\n" +
  364. "        var tmp = parseInt(arguments[i]);\n" +
  365. "        if (isNaN(tmp)) {\n" +
  366. "            var mon = getMonth(arguments[i]);\n" +
  367. "            date2.setMonth(mon);\n" +
  368. "        } else if (tmp < 32) {\n" +
  369. "            date2.setDate(tmp);\n" +
  370. "        } else {\n" +
  371. "            date2.setFullYear(tmp);\n" +
  372. "        }\n" +
  373. "    }\n" +
  374. "    if (adjustMonth) {\n" +
  375. "        date1.setMonth(date.getMonth());\n" +
  376. "        date2.setMonth(date.getMonth());\n" +
  377. "    }\n" +
  378. "    if (isGMT) {\n" +
  379. "    var tmp = date;\n" +
  380. "        tmp.setFullYear(date.getUTCFullYear());\n" +
  381. "        tmp.setMonth(date.getUTCMonth());\n" +
  382. "        tmp.setDate(date.getUTCDate());\n" +
  383. "        tmp.setHours(date.getUTCHours());\n" +
  384. "        tmp.setMinutes(date.getUTCMinutes());\n" +
  385. "        tmp.setSeconds(date.getUTCSeconds());\n" +
  386. "        date = tmp;\n" +
  387. "    }\n" +
  388. "    return ((date1 <= date) && (date <= date2));\n" +
  389. "}\n" +
  390.  
  391. "function timeRange() {\n" +
  392. "    var argc = arguments.length;\n" +
  393. "    var date = new Date();\n" +
  394. "    var isGMT= false;\n"+
  395. "\n" +
  396. "    if (argc < 1) {\n" +
  397. "        return false;\n" +
  398. "    }\n" +
  399. "    if (arguments[argc - 1] == 'GMT') {\n" +
  400. "        isGMT = true;\n" +
  401. "        argc--;\n" +
  402. "    }\n" +
  403. "\n" +
  404. "    var hour = isGMT ? date.getUTCHours() : date.getHours();\n" +
  405. "    var date1, date2;\n" +
  406. "    date1 = new Date();\n" +
  407. "    date2 = new Date();\n" +
  408. "\n" +
  409. "    if (argc == 1) {\n" +
  410. "        return (hour == arguments[0]);\n" +
  411. "    } else if (argc == 2) {\n" +
  412. "        return ((arguments[0] <= hour) && (hour <= arguments[1]));\n" +
  413. "    } else {\n" +
  414. "        switch (argc) {\n" +
  415. "        case 6:\n" +
  416. "            date1.setSeconds(arguments[2]);\n" +
  417. "            date2.setSeconds(arguments[5]);\n" +
  418. "        case 4:\n" +
  419. "            var middle = argc >> 1;\n" +
  420. "            date1.setHours(arguments[0]);\n" +
  421. "            date1.setMinutes(arguments[1]);\n" +
  422. "            date2.setHours(arguments[middle]);\n" +
  423. "            date2.setMinutes(arguments[middle + 1]);\n" +
  424. "            if (middle == 2) {\n" +
  425. "                date2.setSeconds(59);\n" +
  426. "            }\n" +
  427. "            break;\n" +
  428. "        default:\n" +
  429. "          throw 'timeRange: bad number of arguments'\n" +
  430. "        }\n" +
  431. "    }\n" +
  432. "\n" +
  433. "    if (isGMT) {\n" +
  434. "        date.setFullYear(date.getUTCFullYear());\n" +
  435. "        date.setMonth(date.getUTCMonth());\n" +
  436. "        date.setDate(date.getUTCDate());\n" +
  437. "        date.setHours(date.getUTCHours());\n" +
  438. "        date.setMinutes(date.getUTCMinutes());\n" +
  439. "        date.setSeconds(date.getUTCSeconds());\n" +
  440. "    }\n" +
  441. "    return ((date1 <= date) && (date <= date2));\n" +
  442. "}\n"
  443.  
  444.